//
//  WmcAvatarCropMask.swift
//  YPWatermarkCamera
//
//  Created by yupao_ios_macmini06 on 2022/10/28.
//

import UIKit

class WmcAvatarCropMask: WmcBaseView {
 
    override func makeUI() {
        
        self.backgroundColor = UIColor.clear
        self.maskCropArea()
    }
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let hitView = super.hitTest(point, with: event)
        if ((hitView?.isKind(of: WmcAvatarCropMask.classForCoder())) != nil) {
            return nil
        }
        return hitView
    }
    
    func maskCropArea() {
        let outerPath = UIBezierPath(rect: UIScreen.main.bounds)
        let rectPath = UIBezierPath(rect: self.cropRectView.frame)
        UIColor.white.setStroke()
        rectPath.lineWidth = 2
        rectPath.stroke()
        outerPath.usesEvenOddFillRule = true
        outerPath.append(rectPath)
        
        let maskLayer = CAShapeLayer()
        maskLayer.path = outerPath.cgPath
        maskLayer.fillRule = CAShapeLayerFillRule.evenOdd
        maskLayer.fillColor = UIColor.black.withAlphaComponent(0.66).cgColor
        self.layer.addSublayer(maskLayer)
    }
    
    lazy var croppingMaskLayer: CAShapeLayer = {
        let path = UIBezierPath(rect: self.bounds)
        let circlePath = UIBezierPath(arcCenter: self.center, radius: 100, startAngle: 0, endAngle: Double.pi * 2, clockwise: false)
        path.append(circlePath)
        let shapeLayer = CAShapeLayer.init()
        shapeLayer.path = path.cgPath
        return shapeLayer
    }()
    
    
    lazy var cropRectView: UIView = {
        let view = UIView(frame: self.cropRect)
        view.layer.borderWidth = 2
        view.layer.borderColor = UIColor.white.cgColor
        return view
    }()
    
    lazy var cropRect: CGRect = {
        let x: CGFloat = 0
        let y = (Wmc_screenHeight - 50 - Wmc_statusBarHeight)/2.0 - Wmc_screenWidth/2.0
        let rect = CGRect.init(x: x, y: y, width: Wmc_screenWidth, height: Wmc_screenWidth)
        return rect
    }()
    
}
